package com.Beans.Home;

import javax.faces.application.FacesMessage;
import javax.faces.event.ActionEvent;

import com.Beans.Dao.EmpleadoDAO;
import com.Beans.Dao.JefeareaDAO;
import com.Beans.Models.Empleado;
import com.Beans.Models.Jefearea;
import com.Beans.Sessions.Session;



public class Login {

	private String user;
	private String password;
	
	public Login (){
		
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void Ingresar(ActionEvent ae){
		
		Empleado e = EmpleadoDAO.findByLogin(user, password);
		if(e == null){
			Jefearea ja = JefeareaDAO.findByLogin(user, password);
			if(ja == null){
				System.out.println("ERROR");
			}else{
				String cargo = "jefearea";
				Session.addSession(ja.getIdjefearea(), ja.getNombres(), ja.getApellidos(), ja.getCorreo(), ja.getUser(), cargo, ja.getArea().getIdArea());
				Session.getFacesContext().addMessage(null, new FacesMessage(FacesMessage.FACES_MESSAGES,"Bienvenido "+ Session.getNombre()));
				Session.redirectTo("/jefearea/solicitudes.xhtml");
			}
		}else{
			String cargo = "empleado";
			Session.addSession(e.getIdEmpleado(), e.getNombre(), e.getApellido(), e.getCorreo(), e.getUser(), cargo, e.getArea().getIdArea());
			Session.getFacesContext().addMessage(null, new FacesMessage(FacesMessage.FACES_MESSAGES,"Bienvenido "+ Session.getNombre()));
			Session.redirectTo("/empleado/solicitudes.xhtml");
		}
	}

}